

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;


public class AopClassAdapter extends ClassVisitor implements Opcodes {
    public AopClassAdapter(int api, ClassWriter classWriter) {
        super(api, classWriter);
    }
    public void visit(int version, int access, String name,
                         String signature, String superName, String[] interfaces) {
        super.visit(version,access,name,signature,superName,interfaces);
    }
    public MethodVisitor visitMethod(int access, String name,
                                     String desc, String signature, String[] exceptions) {
        MethodVisitor mv = super.visitMethod(access, name,
                                          desc, signature, exceptions);
        MethodVisitor visitor = new MethodVisitor(this.api, mv) {
            /**
             * 访问方法头，只访问一次
             */
            @Override
            public void visitCode() {
                super.visitCode();
                this.visitMethodInsn(INVOKESTATIC, "InvokeTimer", "start", "()V",false);
            }

            /**
             * 返回
             * @param opcode
             */
            @Override
            public void visitInsn(int opcode) {
                if (opcode == RETURN) {//在返回之前安插after 代码。
                    mv.visitMethodInsn(INVOKESTATIC, "InvokeTimer", "end", "()V",false);
                }
                super.visitInsn(opcode);
            }
        };


        return visitor;
    }
}
